package net.qhzw.vip.qiyu.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.dto.CommunityDto;
import net.qhzw.vip.qiyu.entity.SysCommunity;
import net.qhzw.vip.qiyu.entity.SysUser;
import net.qhzw.vip.qiyu.handler.MyException;
import net.qhzw.vip.qiyu.mapper.SysCommunityMapper;
import net.qhzw.vip.qiyu.result.PageResult;
import net.qhzw.vip.qiyu.result.ResultCode;
import net.qhzw.vip.qiyu.service.SysCommunityService;
import net.qhzw.vip.qiyu.service.SysUserService;
import net.qhzw.vip.qiyu.util.PageAdapter;
import org.apache.axis.utils.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

@Service
@Slf4j
public class SysCommunityServiceImpl extends ServiceImpl<SysCommunityMapper, SysCommunity>
        implements SysCommunityService {


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private SysCommunityMapper communityMapper;

    @Autowired
    private SysCommunityMapper sysCommunityMapper;

    @Autowired
    private SysUserService sysUserService;

    /***
     * 分页查询社区
     * @param page 分页参数
     * @param name 社区名称
     * @return
     */
    @Override
    public PageResult<CommunityDto> findByPage(Page<SysCommunity> page, String name, Long shopId) {
        List<CommunityDto> communityDtos = sysCommunityMapper.findByPage(new PageAdapter(page), name, shopId);
        Long total = sysCommunityMapper.pageCount(name, shopId);
        return new PageResult<CommunityDto>(total, communityDtos);
    }

    //已租电池骑遇
    private Long getRentedBatteryQ(Long id) {
        return sysCommunityMapper.getRentedBatteryQ(id);
    }

    //已租电池骑缘
    private Long getRentedBatteryP(Long id) {
        return sysCommunityMapper.getRentedBatteryP(id);
    }

    //已租车辆骑缘
    private Long getRentedVehiclePY(Long id) {
        return sysCommunityMapper.getRentedVehiclePY(id);
    }

    /**
     * 已租车辆骑遇
     *
     * @param id
     * @return
     */
    private Long getRentedVehicleQY(Long id) {
        return sysCommunityMapper.getRentedVehicleQY(id);
    }


    /**
     * 损坏车辆
     *
     * @param id
     * @return
     */
    private Long getDamageTheVeh(Long id) {
        return sysCommunityMapper.getDamageTheVeh(id);
    }

    //总库量
    private Long getTotalInventory(Long id) {
        return sysCommunityMapper.getTotalInventory(id);
    }

    /***
     * 生成社区二维码
     * @param id 社区id
     * @return
     */
    @Override
    public String createQrCode(String id) {
        String result = "data:image/jpeg;base64,";
        SysCommunity community = getById(id);
        if (community == null) {
            return "生成失败, 请退出重新登录";
        }
        String string = JSON.toJSONString(community);
        BufferedImage bufferedImage = QrCodeUtil.generate(string,
                450,
                450);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, "png", stream);
            String encode = Base64.encode(stream.toByteArray());
            return result + encode;
        } catch (IOException e) {
            throw new MyException(ResultCode.YUNAVAILABLE);
        }
    }

    /***
     * 根据社区名称查询社区id
     * @param communityName
     * @return
     */
    @Override
    public String findIdByCommunityName(String communityName) {
        return communityMapper.findIdByCommunityName(communityName);
    }

    /***
     * 根据社区id查询社区名称
     * @param communityId
     * @return
     */
    @Override
    public String getCommunityNameById(Long communityId) {
        SysCommunity community = getById(communityId);
        if (community != null) {
            return community.getName();
        }
        return null;
    }

    /***
     * 根据站点id查询站点地址
     * @param communityId
     * @return
     */
    @Override
    public String findAddressById(String communityId) {
        SysCommunity community = getById(communityId);
        if (community != null) {
            return community.getAddress();
        }
        return null;
    }


    /****
     * 根据站点管理员获取站点id
     * @param adminId
     * @return
     */
    @Override
    public Long getIdByAdmin(Long adminId) {
        return communityMapper.getIdByAdmin(adminId);
    }

    /***
     * 根据租车userId查询租车站点电话
     * @param userId
     * @return
     */
    @Override
    public String getSiteMobile(Long userId) {
        return communityMapper.getSiteMobile(userId);
    }


    /***
     * 根据系统当前登录人获取站点ID
     * @param userId
     * @return
     */
    @Override
    public Long getCommunityId(Long userId) {
        SysUser sysUser = sysUserService.getById(userId);
        if (sysUser == null)
            return null;
        return sysUser.getCommunityId();
    }

    @Override
    public SysCommunity findByDtsUserId(Long dtsUserId) {
        return communityMapper.findByDtsUserId(dtsUserId);
    }



  /*  public static void main(String[] args) throws IOException {
        BufferedImage generate = QrCodeUtil.generate("https://",
                300,
                300);
        String base64 = "data:image/jpeg;base64,";
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ImageIO.write(generate, "png", stream);
        String encode = Base64.encode(stream.toByteArray());

        System.out.println(base64 + encode);
    }*/
}
